/**
 * Created by hcadmin on 16/4/18.
 */
define([
    "jquery",
    "IO",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
    "bootbox"
], function ($, IO, upui, up, jui, bootbox, definebt) {
    $(function () {

        gettable();


        //新增体检
        $("#addtemp").click(function(){
            var testtemp_name=$("#testtemp_name").val();
            var testtemp_days=$("#testtemp_days").val();
            var testtemp_attention=$("#testtemp_attention").val();
            var testtemp_introduction=$("#testtemp_introduction").val();

            var params = {
                testtemp_name:testtemp_name,
                testtemp_days:testtemp_days,
                testtemp_attention:testtemp_attention,
                testtemp_introduction:testtemp_introduction
            };
            if(testtemp_name.length<=0){
                bootbox.alert('体检名称不能为空');
                return;
            }
            if(!isNaN(testtemp_days)&&testtemp_days.length<=0){
                bootbox.alert('体检距怀孕日时间格式错误');
                return;
            }
            if(testtemp_attention.length<=0){
                bootbox.alert('体检注意事项不能为空');
                return;
            }
            if(testtemp_introduction.length<=0){
                bootbox.alert('体检介绍不能为空');
                return;
            }

            IO.post("/api/testtemps",params,function(d){
                if(d.code=="0"){
                    bootbox.alert('新增成功');
                    $('#myModal').modal('hide');
                    gettable();
                }
                else {
                    bootbox.alert('新增失败');
                    $('#myModal').modal('hide');
                    alert(d.msg);
                }
            })

        });

        //修改体检
        $("#edittemp").click(function(){

            var testtemp_name=$("#testtemp_name_edit").val();
            var testtemp_days=$("#testtemp_days_edit").val();
            var testtemp_attention=$("#testtemp_attention_edit").val();
            var testtemp_introduction=$("#testtemp_introduction_edit").val();
            var id=$("#hidden_id").val();

            var params = {
                testtemp_name:testtemp_name,
                testtemp_days:testtemp_days,
                testtemp_attention:testtemp_attention,
                testtemp_introduction:testtemp_introduction,
                id:id
            };
            IO.post("/api/testtemps/edit",params,function(d){
                if(d.code=="0"){
                    bootbox.alert('修改成功');
                    $('#myModaledit').modal('hide');
                    gettable();
                }
                else {
                    bootbox.alert('修改失败');
                    $('#myModaledit').modal('hide');
                    alert(d.msg);
                }
            })

        });

        $('#myModal').on('hide.bs.modal', function () {
            $("#testtemp_name").val("");
            $("#testtemp_days").val("");
            $("#testtemp_attention").val("");
            $("#testtemp_introduction").val("");
        })

    });
    function gettable(){

        var grid;
        //每列数据列性
        var columns = [
            {
                title: '序号',
                dataIndex: 'testtemp_id',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].testtemp_id + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '体检名称',
                dataIndex: 'testtemp_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].testtemp_name + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '<small>'+'体检计算时间'+'(据怀孕日期的天数计算)'+'</small>',
                dataIndex: 'testtemp_days',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].testtemp_days + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '体检注意事项',
                dataIndex: 'testtemp_attention',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].testtemp_attention.substring(0,20) +"..."+ "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '发布日期',
                dataIndex: 'createdDate',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return '<span>'+data[datarow].createdDate+'</span>'

                }
            },
            {
                title: '体检介绍',
                dataIndex: 'testtemp_introduction',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<a href='#' act_id="+data[datarow].testtemp_id+" class='detailmodal'><span style='color: #6ab7ad;'>" + "详情" + "</span></a>";
                }
            },
            {
                title: '操作',
                dataIndex: 'action',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<a href='#' act_id="+data[datarow].testtemp_id+" class='editmodal' ><span style='color: #d47c7b;'>" + "编辑" + "</span></a>&nbsp;&nbsp;"+
                        "<a href='#' act_id="+data[datarow].testtemp_id+" class='delete'><span style='color: #6ab7ad;'>" + "删除" + "</span></a>";
                }
            }
        ];
        //数组初始化

        function dataInit() {

            grid = new Grid("/api/testtemps/list", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 10);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {

            var param="";
            return param;
        }
        dataInit();
    }
    //删除模板
    $(document).on("click",".delete",function(){
        var act_id=$(this).attr("act_id");
        if(act_id){
            bootbox.confirm("确定要删除吗？",function(result){
                if(result) {
                    IO.get("/api/testtemps/delete",{id:act_id},function(d){
                        //console.log(d);
                        if(d.code==0){
                            gettable();
                        }
                    });
                }
            });
        }
    });

    $(document).on("click",".detailmodal",function(){
        var act_id=$(this).attr("act_id");
        if(act_id){
            //先查数据绑定到页面上,然后手动调用modal
            IO.get("/api/tests",{id:act_id},function(d){
                var data= d.data;
                $('#myModaldetail input').attr('readonly','readonly');
                $('#myModaldetail textarea').attr('readonly','readonly');
                $("#testtemp_name_detail").val(data.testtemp_name);
                $("#testtemp_days_detail").val(data.testtemp_days);
                $("#testtemp_attention_detail").val(data.testtemp_attention);
                $("#testtemp_introduction_detail").val(data.testtemp_introduction);

                $('#myModaldetail').modal();
            });

        }
    });

    $(document).on("click",".editmodal",function(){
        var act_id=$(this).attr("act_id");
        $("#hidden_id").val(act_id);
        if(act_id){

            IO.get("/api/tests",{id:act_id},function(d){
                var data= d.data;
                $("#testtemp_name_edit").val(data.testtemp_name);
                $("#testtemp_days_edit").val(data.testtemp_days);
                $("#testtemp_attention_edit").val(data.testtemp_attention);
                $("#testtemp_introduction_edit").val(data.testtemp_introduction);
                $('#myModaledit').modal();
            });
        }
    });

});

